﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TasksUI
{
    public partial class GoogleSyncConfig : Form
    {
        public GoogleSyncConfig()
        {
            InitializeComponent();
            cbCalendar.DisplayMember = "Title";
            cbCalendar.ValueMember = "Url";
        }

        public GoogleSyncConfig(TasksModel.TaskLibrary library)
            : this()
        {
            TasksModel.GoogleCalendarSynchSettings set = library.SynchSettings as TasksModel.GoogleCalendarSynchSettings;
            if (set == null)
                _settings = new TasksModel.GoogleCalendarSynchSettings();
            else
            {
                _settings = new TasksModel.GoogleCalendarSynchSettings(set);
                // fill
                tbLogin.Text = _settings.Login;
                tbPassword.Text = _settings.Password;
                if (_settings.IsConfigured)
                {
                    List<TasksUI.Helpers.GoogleHelper.CalendarWrapper> l = new List<TasksUI.Helpers.GoogleHelper.CalendarWrapper>();
                    l.Add(new TasksUI.Helpers.GoogleHelper.CalendarWrapper(_settings.Calendar));
                    cbCalendar.DataSource = l;
                    cbCalendar.Enabled = true;
                    bConnect.Enabled = true;
                    bApply.Enabled = true;
                }
                else
                    bConnect.Enabled = tbLogin.Text.Length > 0 && tbPassword.Text.Length > 0;
            }
        }

        private TasksModel.GoogleCalendarSynchSettings _settings;

        public static void Show(TasksModel.TaskLibrary library)
        {
            using (GoogleSyncConfig f = new GoogleSyncConfig(library))
            {
                if (f.ShowDialog(Form.ActiveForm) == DialogResult.OK)
                    library.SynchSettings = f._settings;
            }
        }

        private void bConnect_Click(object sender, EventArgs e)
        {
            cbCalendar.Enabled = false;
            bApply.Enabled = false;
            try
            {
                TasksUI.Helpers.GoogleHelper g = new TasksUI.Helpers.GoogleHelper();
                g.UseSSL = true;
                // connect
                if (g.Connect(tbLogin.Text, tbPassword.Text))
                {
                    // get calendars
                    List<TasksUI.Helpers.GoogleHelper.CalendarWrapper> cals = g.GetCalendars();
                    // show calendars
                    cbCalendar.DataSource = cals;
                    if (cals.Count == 0)
                    {
                        MessageBox.Show("There are no calendars owned by you.\nCreate one and proceed.");
                        return;
                    }
                    cbCalendar.SelectedIndex = 0;
                    cbCalendar.Enabled = true;
                    bApply.Enabled = true;
                }
            }
            catch (Google.GData.Client.LoggedException ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void bApply_Click(object sender, EventArgs e)
        {
            if (cbCalendar.SelectedItem != null)
            {
                _settings.Calendar = cbCalendar.SelectedValue.ToString();
                _settings.Login = tbLogin.Text;
                _settings.Password = tbPassword.Text;
                if (_settings.IsConfigured)
                    DialogResult = DialogResult.OK;
            }
        }

        private void tbLogin_TextChanged(object sender, EventArgs e)
        {
            bConnect.Enabled = tbLogin.Text.Length > 0 && tbPassword.Text.Length > 0;
        }
    }
}
